package com.itsu.spbspider.util;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

@Slf4j
public class TaskUtil {

    public static SchedulerFactory schedulerFactory = null;
    public static Scheduler scheduler = null;

    static {
        schedulerFactory = new StdSchedulerFactory();
        try {
            scheduler = schedulerFactory.getScheduler();
        } catch (SchedulerException e) {
            log.warn("task scheduler error", e);
        }
    }

    public static void startJob(Class task, String jobName, Integer intervalSeconds, Integer repeatTime, Date startAt, Date endAt, JobDataMap jobDataMap) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(task).withIdentity(jobName).setJobData(jobDataMap).build();
        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(jobName + "trigger")
                .startAt(startAt)
                .endAt(endAt)
                .withSchedule(SimpleScheduleBuilder
                        .simpleSchedule()
                        .withIntervalInSeconds(intervalSeconds)
                        .withRepeatCount(repeatTime))
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    }

    public static void startJob(Class task, String jobName, Integer intervalSeconds, Integer repeatTime, JobDataMap jobDataMap) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(task).withIdentity(jobName).setJobData(jobDataMap).build();
        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(jobName + "trigger")
                .withSchedule(SimpleScheduleBuilder
                        .simpleSchedule()
                        .withIntervalInSeconds(intervalSeconds)
                        .withRepeatCount(repeatTime))
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    }

    public static void startJob(Class task, String jobName, Integer intervalSeconds) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(task).withIdentity(jobName).build();
        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(jobName + "trigger")
                .withSchedule(SimpleScheduleBuilder
                        .simpleSchedule()
                        .withIntervalInSeconds(intervalSeconds)
                        .repeatForever())
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    }

    public static void startJob(Class task, String jobName, Integer intervalSeconds, JobDataMap jobDataMap) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(task).withIdentity(jobName).setJobData(jobDataMap).build();
        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(jobName + "trigger")
                .withSchedule(SimpleScheduleBuilder
                        .simpleSchedule()
                        .withIntervalInSeconds(intervalSeconds)
                        .repeatForever())
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    }

    public static void stopJob(String jobName) throws SchedulerException {
        TriggerKey triggerKey = new TriggerKey(jobName + "trigger");
        scheduler.pauseTrigger(triggerKey);
        scheduler.unscheduleJob(triggerKey);
        scheduler.deleteJob(JobKey.jobKey(jobName));
    }

    public static Trigger.TriggerState checkJobScheduler(String jobName) throws SchedulerException {
        Trigger.TriggerState triggerState = scheduler.getTriggerState(TriggerKey.triggerKey(jobName + "trigger"));
        return triggerState;
    }

    public static void shutdown(boolean force) throws SchedulerException {
        if (!scheduler.isShutdown()) {
            scheduler.shutdown(force);
        }
    }

}
